<template>
  <el-form style="margin-top:38px;" ref="form" :model="changeuser" :rules="rules" label-width="80px" label-position="top">
    <!-- <el-form-item label=" 姓名" prop="nickName">
      <el-input v-model="user.nickName" placeholder="请输入" :disabled="true" />
    </el-form-item>-->
    <el-form-item label="新手机号码" prop="phonenumber">
      <el-input v-model="changeuser.phonenumber" maxlength="11" placeholder="请输入" />
    </el-form-item>
    <el-form-item label="验证码" prop="inputCode">
      <el-input v-model="inputCode" placeholder="请输入" style="width:75%" auto-complete="off" />
      <el-button class="codebutton" v-if="!isDisabled" @click="sendCode">获取验证码</el-button>
      <el-button class="codebutton disabled" v-if="isDisabled">{{codeTime}}s后重新获取</el-button>
    </el-form-item>
    <!-- <el-form-item label="邮箱修改" prop="email">
      <el-input v-model="changeuser.email" maxlength="50" placeholder="请输入" />
    </el-form-item>-->
    <!-- <el-form-item label="性别">
      <el-radio-group v-model="user.sex">
        <el-radio label="0">男</el-radio>
        <el-radio label="1">女</el-radio>
      </el-radio-group>
    </el-form-item>-->
    <el-form-item class="buttonItem">
      <el-button @click="close" style="color: #1890FF;">关闭</el-button>
      <el-button type="primary" @click="submit">保存</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { updateUserProfile, getUserProfile } from "@/api/system/user";
import axios from "axios";
export default {
  props: {
    user: {
      type: Object
    }
  },
  data () {
    return {
      changeuser: {},
      isDisabled: false,
      codeTime: 60,
      infoCode: '',
      inputCode: '',
      // 表单校验
      rules: {
        // nickName: [
        //   { required: true, message: "用户昵称不能为空", trigger: "blur" }
        // ],
        // email: [
        //   { required: true, message: "邮箱地址不能为空", trigger: "blur" },
        //   {
        //     type: "email",
        //     message: "'请输入正确的邮箱地址",
        //     trigger: ["blur", "change"]
        //   }
        // ],
        phonenumber: [
          { required: true, message: "手机号码不能为空", trigger: "blur" },
          {
            pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
            message: "请输入正确的手机号码",
            trigger: "blur"
          }
        ]
      }
    };
  },
  created () {
    getUserProfile().then(response => {
      this.changeuser = response.data;
    });
  },
  methods: {
    sendCode () {
      axios.get(process.env.VUE_APP_BASE_API + '/captchaSMS', { params: { phonenumber: this.changeuser.phonenumber } }).then(res => {
        if (res.data.code === 200) {
          this.infoCode = res.data.verifyCode
          this.isDisabled = true;
          let vm = this
          const codeTime = 60;
          let timer = window.setInterval(() => {
            if (vm.codeTime > 0 && vm.codeTime <= codeTime) {
              // 倒计时时不可点击
              vm.isDisabled = true
              // 计时秒数
              vm.codeTime--
            } else {
              // 倒计时完，可点击
              vm.isDisabled = false
              // 更新按钮文字内容
              // 清空定时器!!!
              clearInterval(timer)
              vm.codeTime = 60;
            }
          }, 1000)
        }
      })
    },
    submit () {
      if (this.user.phonenumber === this.changeuser.phonenumber) {
        this.msgError("新手机号与旧手机号不能重复，请输入新的手机号！");
       /* this.$refs["form"].validate(valid => {
          if (valid) {
            updateUserProfile(this.changeuser).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.$emit('updateInfo')
              } else {
                this.msgError(response.msg);
              }
            });
          }
        });*/
      } else {
        if (this.inputCode.trim() !== '') {
          if (this.inputCode === this.infoCode) {
            this.$refs["form"].validate(valid => {
              if (valid) {
                updateUserProfile(this.changeuser).then(response => {
                  if (response.code === 200) {
                    this.msgSuccess("修改成功");
                    this.$emit('updateInfo')
                  } else {
                    this.msgError(response.msg);
                  }
                });
              }
            });
          } else {
            this.msgError("验证码错误")
          }
        } else {
          this.msgError("验证码不能为空")
        }
      }
    },
    close () {
      this.$store.dispatch("tagsView/delView", this.$route);
      this.$router.push({ path: "/index" });
    }
  }
};
</script>
<style lang="scss" scoped>
/deep/.el-form-item.is-required:not(.is-no-asterisk)
  > .el-form-item__label:before,
.el-form-item.is-required:not(.is-no-asterisk)
  .el-form-item__label-wrap
  > .el-form-item__label:before {
  content: '';
  margin-right: 0;
}

.el-form {
  margin: 8px 36px 8px 16px;
  .el-form-item {
    margin-bottom: 40px;
    /deep/.el-form-item__label {
      font-family: Microsoft YaHei;
      font-size: 16px;
      color: rgba(0, 0, 0, 0.65);
      letter-spacing: 0;
      line-height: 14px;
      font-weight: 500;
      padding: 0 0 14px 0;
    }
    .el-radio__inner {
      width: 20px;
      height: 18px;
    }
    .el-radio__inner::after {
      width: 6px;
      height: 6px;
    }
    .el-button--medium {
      font-family: PingFangSC-Regular;
      padding: 12px 34px;
    }
    .el-button + .el-button {
      margin-left: 26px;
    }
  }
  .buttonItem {
    margin-bottom: 48px;
    position: absolute;
    bottom: 0;
  }
}
/deep/.el-input--medium .el-input__inner {
  height: 40px;
  line-height: 40px;
}
.codebutton {
  width: calc(25% - 8px);
  background-color: #ffffff;
  font-family: PingFangSC-Regular;
  font-size: 14px;
  color: rgba(0, 0, 0, 0.85);
  letter-spacing: 0px;
  height: 40px;
  padding: 0 !important;
}

.disabled {
  color: #c0c4cc;
  cursor: not-allowed;
  background-image: none;
  background-color: #fff;
  border-color: #e6ebf5;
}
</style>

